import { _decorator, Color, Component, Node, Sprite, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Food')
export class Food extends Component {
    start() {
        
        //食物出现的位置
        this.node.setPosition(this.randomPos());

        //食物出现时的颜色
        this.node.getComponent(Sprite).color = this.randomColor();

    }

    update(deltaTime: number) {
        
    }

    //食物颜色随机
    randomColor(){
        //颜色是由红、绿、蓝三种颜色,通过改三种颜色的值得到新的颜色
        let red = Math.round(Math.random()*255);
        let green = Math.round(Math.random()*255);
        let blue = Math.round(Math.random()*255);

        return new Color(red,green,blue);

    }
    //随机食物的位置
    randomPos(){

        //获取游戏界面的宽、高
        let width = this.node.parent.getComponent(UITransform).contentSize.width-300;
        let height = this.node.parent.getComponent(UITransform).contentSize.height-300;

        //食物出现的位置在画布的中心点，即以画布的的中心为原点移动
        let x = Math.round(Math.random()*width)-width/2;
        let y = Math.round(Math.random()*height)-height/2;

        return new Vec3(x,y,0);
    }
}


